package com.chenl.provider.DEMOS.Spring.Spring事务相关.事务监听器;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
public class MyTransactionEventListener {//事务监听器，在开启事务并且发布了事件才能触发监听得到
	/**
	 * 事务回滚后
	 */
	@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
	public void afterRollback(String name) {
		log.info("事务回滚 后打印了:" + TransactionThreadLocal.get());
		log.info("事务回滚 后印了:" + name);
	}
	/**
	 * 事务提交前
	 */
	@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
	public void beforeCommit(String name) {
		log.info("事务提交 前打印了:" + TransactionThreadLocal.get());
		log.info("事务提交 前打印了:" + name);
	}
	/**
	 * 事务提交后
	 */
	@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
	public void afterCommit(String name) {
		log.info("事务提交 后打印了:" + TransactionThreadLocal.get());
		log.info("事务提交 后打印了:" + name);
	}
	/**
	 * 事务完成后
	 */
	@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
	public void afgerCompletion(String name) {
		log.info("事务完成 后打印了:" + TransactionThreadLocal.get());
		log.info("事务完成 后打印了:" + name);
	}
}
